/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.WorkProcessBean;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.WorkProcessDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.WorkCenter;
import com.minierp.model.WorkProcess;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class WorkProcessMACtr extends MultiActionController {
	private WorkProcessDao workProcessDao;
	private DepartmentDao departmentDao;
	private String listView;
	private String listForSelectView;
	private String setPercentFormView;
	private String dialogSuccessView;

	public String getListForSelectView() {
		return this.listForSelectView;
	}

	public void setListForSelectView(String listForSelectView) {
		this.listForSelectView = listForSelectView;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public String getSetPercentFormView() {
		return this.setPercentFormView;
	}

	public void setSetPercentFormView(String setPercentFormView) {
		this.setPercentFormView = setPercentFormView;
	}

	public String getDialogSuccessView() {
		return this.dialogSuccessView;
	}

	public void setDialogSuccessView(String dialogSuccessView) {
		this.dialogSuccessView = dialogSuccessView;
	}

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listView);
		String wcId = request.getParameter("wc_id");
		List list = this.workProcessDao.getList(Long.parseLong(wcId), true);
		mv.addObject("list", list);
		mv.addObject("workCenter",
				this.workProcessDao.get(WorkCenter.class, Long.parseLong(wcId)));
		mv.addObject("depList", this.departmentDao.getList(true));
		return mv;
	}

	public ModelAndView handleListForSelect(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForSelectView);
		String wcId = request.getParameter("wc_id");
		List list = this.workProcessDao.getList(Long.parseLong(wcId), true);
		mv.addObject("list", list);
		return mv;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String retUrl = request.getParameter("from_url");
		ModelAndView mv = new ModelAndView("redirect:" + retUrl);
		WorkProcess p = (WorkProcess) this.workProcessDao.get(
				WorkProcess.class, Long.parseLong(request.getParameter("id")));
		if (p != null) {
			p.setEnabled("N");
			this.workProcessDao.update(p);
		}

		EncCommonHelper.setAlertMessage(request, "删除成功！");
		return mv;
	}

	public ModelAndView handleSetPercentForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.setPercentFormView);
		String wcId = request.getParameter("wc_id");
		List list = this.workProcessDao.getList(Long.parseLong(wcId), true);
		ArrayList arr = new ArrayList();

		for (int i = 0; i < list.size(); ++i) {
			WorkProcess p = (WorkProcess) list.get(i);
			WorkProcessBean bean = new WorkProcessBean();
			bean.setWorkProcess(p);
			bean.setPercentStr(String.valueOf(p.getPercent()));
			arr.add(bean);
		}

		mv.addObject("list", arr);
		mv.addObject("workCenter",
				this.workProcessDao.get(WorkCenter.class, Long.parseLong(wcId)));
		return mv;
	}

	public ModelAndView handleSetPercent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String wcId = request.getParameter("wc_id");
		List list = this.workProcessDao.getList(Long.parseLong(wcId), true);
		BigDecimal zero = new BigDecimal(0);
		BigDecimal one = new BigDecimal(1);
		BigDecimal total = new BigDecimal(0);
		String errMsg = "";

		int arr;
		for (arr = 0; arr < list.size(); ++arr) {
			WorkProcess i = (WorkProcess) list.get(arr);
			String p = request.getParameter("percent_" + i.getId());
			if (p != null) {
				try {
					i.setPercent(new BigDecimal(p));
				} catch (Exception arg14) {
					errMsg = "『产值比重』只能输入数字";
				}
			}

			if (errMsg.equals("")
					&& (i.getPercent().compareTo(zero) < 0 || i.getPercent()
							.compareTo(one) > 0)) {
				errMsg = "『产值比重』不能输入大于1或小于0的数字";
			}

			total = total.add(i.getPercent());
		}

		if (errMsg.equals("")
				&& (total.compareTo(zero) < 0 || total.compareTo(one) > 0)) {
			errMsg = "『产值比重』总和不能小于0或超过1";
		}

		ModelAndView mv;
		if (errMsg.equals("")) {
			for (arr = 0; arr < list.size(); ++arr) {
				this.workProcessDao.update((WorkProcess) list.get(arr));
			}

			mv = new ModelAndView(this.dialogSuccessView);
			mv.addObject("returnValue", wcId);
			EncCommonHelper.setAlertMessage(request, "保存成功");
		} else {
			ArrayList arg15 = new ArrayList();

			for (int arg16 = 0; arg16 < list.size(); ++arg16) {
				WorkProcess arg17 = (WorkProcess) list.get(arg16);
				WorkProcessBean bean = new WorkProcessBean();
				bean.setWorkProcess(arg17);
				String percent = request.getParameter("percent_"
						+ arg17.getId());
				if (percent != null) {
					bean.setPercentStr(percent);
				} else {
					bean.setPercentStr(String.valueOf(arg17.getPercent()));
				}

				arg15.add(bean);
			}

			mv = new ModelAndView(this.setPercentFormView);
			mv.addObject("list", arg15);
			mv.addObject(
					"workCenter",
					this.workProcessDao.get(WorkCenter.class,
							Long.parseLong(wcId)));
			EncCommonHelper.setAlertMessage(request, errMsg);
		}

		return mv;
	}

	public ModelAndView handleGetWorkCenterProcess(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Document doc = DocumentHelper.createDocument();
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		String wcId = request.getParameter("wc_id");
		List list = this.workProcessDao.getList(Long.parseLong(wcId), true);
		Element root = doc.addElement("body");

		for (int out = 0; out < list.size(); ++out) {
			WorkProcess output = (WorkProcess) list.get(out);
			Element process = root.addElement("process");
			Element id = process.addElement("id");
			id.setText(String.valueOf(output.getId()));
			Element proName = process.addElement("proName");
			proName.setText(output.getProcessName());
		}

		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ServletOutputStream arg12 = response.getOutputStream();
		XMLWriter arg13 = new XMLWriter(arg12, format);
		arg13.write(doc);
		arg12.flush();
		arg12.close();
		return null;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public WorkProcessDao getWorkProcessDao() {
		return this.workProcessDao;
	}

	public void setWorkProcessDao(WorkProcessDao workProcessDao) {
		this.workProcessDao = workProcessDao;
	}
}